#include <iostream>
#include <string.h>
#include <memory>
using namespace std;
//认识智能指针的删除器
void test(){
    FILE* fp=fopen("hello.txt","a+");
    const char* pstr="hello,world";
    fwrite(pstr,strlen(pstr),1,fp);
    fclose(fp);
}
void test2(){
    unique_ptr<FILE> up(fopen("wd.txt","a+"));
    string content="hello,c++";
    fwrite(content.c_str(),content.size(),1,up.get());
    fclose(up.get());
}
void test3(){
    shared_ptr<FILE> sp(fopen("wd.txt","a+"));
    string content="hello";
    fwrite(content.c_str(),content.size(),1,sp.get());
    //fclose(sp.get());
}

class FileDeletor{
public:
    void operator()(FILE*fp){
        if(fp){
            fclose(fp);
        }
    }
};
void test4(){
    unique_ptr<FILE,FileDeletor> up(fopen("wd.txt","a+"));
    string content="hello,c++";
    fwrite(content.c_str(),content.size(),1,up.get());
}
void test5(){
    FileDeletor fd;
    shared_ptr<FILE> sp(fopen("wd.txt","a+"),fd);
    string content="hello,c++";
    fwrite(content.c_str(),content.size(),1,sp.get());

}
int main(int argc, char * argv[]){
    test5();
    return 0;
}

